const app = getApp()
import{getUrl}from "../../../utils/url";
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:'',
    address:'',
    mo:true
  },
  dchange(e){
    this.setData({
      mo:e.detail.value
    })
  },
  navTo(e){
    app.com.navTo(e)
  },
  bInput(e){
    this.setData({
      detail:e.detail.value
    })
  },
  submit(){
    if(this.data.address == ''){
      wx.showToast({
        title: '请选择楼栋',
        icon:'none'
      })
    }else if(this.data.detail == ''){
      wx.showToast({
        title: '请输入门牌号',
        icon:'none'
      })
    }else{
      wx.showLoading({
        title: '保存中',
        task:true
      })
      let url = '/address/save'
      let formData = {
        address: this.data.address,
        detail: this.data.detail,
      }
      if(this.data.id){
        url = '/address/update'
        formData.id = this.data.id
        this.setMo(this.data.id);
      }else{
        formData.userId = wx.getStorageSync("user").id;
      }
      wx.request({
        url: getUrl()+url,  //路径
        data:formData,//参数
        success: function(res) {
       console.log(res);
        }
      })
      

        wx.hideLoading()
          wx.showToast({
            title: '保存成功',
            task:true
          })
    }
  },
  setMo(id){
    if(this.data.mo){
   let user=  wx.getStorageSync('user');
     user.default_address=id;
        wx.setStorageSync('user', user);
    }else{
      let user=  wx.getStorageSync('user');
      user.default_address=0;
         wx.setStorageSync('user', user);
    }

    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this
    if(options.data){
      let msg = JSON.parse(options.data)
      _this.setData({
        detail:msg.detail,
        address:msg.address,
        mo:msg.id == wx.getStorageSync("user").default_address,
        id:msg.id
      })
    }
  },

  
})